import 'package:erp/widgets/load_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SaleProjectCard extends StatelessWidget {
  final Map<String, dynamic> saleProject;

  final void Function(Map<String, dynamic> saleProject)? detailTap;

  SaleProjectCard({required this.saleProject, this.detailTap});

  _buildContractTag(Map<String, dynamic> saleProject) {
    int contractStatus = saleProject["contractStatus"] as int;
    var color;
    if (contractStatus == 0) {
      color = Color(0xff4990FF);
    } else if (contractStatus == 1) {
      color = Color(0xff36D1C7);
    } else {
      color = Colors.grey;
    }
    return _buildTag(saleProject['contractStatusName'], color);
  }

  _buildBidStatus(Map<String, dynamic> saleProject) {
    String bidStatus = saleProject["bidStatus"] as String;
    if (bidStatus == "BIDDING") {
      return _buildTag(saleProject['bidStatusName'], Color(0xff36D1C7));
    }
    if (bidStatus == "WIN") {
      return _buildTag(saleProject['bidStatusName'], Color(0xffFA8541));
    } else {
      return SizedBox.shrink();
    }
  }

  _buildTag(String text, Color bgColor) {
    return Container(
      width: 100.w,
      height: 32.h,
      alignment: AlignmentDirectional.center,
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.all(
          Radius.circular(15.r),
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: Colors.white,
          fontSize: 20.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints(minHeight: 270.h, maxWidth: 686.w),
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 29.h, horizontal: 23.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(
            Radius.circular(10.r),
          ),
          boxShadow: [
            BoxShadow(
              offset: Offset(0, 1.w),
              blurRadius: 8.r,
              spreadRadius: 0,
              color: Color.fromRGBO(54, 95, 179, 0.2),
            )
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(
                  saleProject['projectNo'],
                  style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 32.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(width: 36.w),
                _buildContractTag(saleProject),
                SizedBox(width: 12.w),
                _buildBidStatus(saleProject),
                Spacer(),
                GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    if (detailTap != null) {
                      detailTap!(saleProject);
                    }
                  },
                  child: Container(
                    alignment: AlignmentDirectional.center,
                    width: 32.w,
                    child: LoadAssetImage('sale_project/detail', width: 6.w, height: 26.h),
                  ),
                ),
              ],
            ),
            SizedBox(height: 12.h),
            Text(
              saleProject['projectName'],
              style: TextStyle(color: Color(0xff666666), fontSize: 28.sp),
            ),
            SizedBox(height: 29.h),
            Text(
              '项目类别：${saleProject['projectCategoryName']}',
              style: TextStyle(color: Color(0xff999999), fontSize: 24.sp),
            ),
            Text(
              '所属大区：${saleProject['areaDeptName']}',
              style: TextStyle(color: Color(0xff999999), fontSize: 24.sp),
            ),
            SizedBox(height: 26.h),
            Row(
              children: [
                Expanded(
                  child: LayoutBuilder(
                    builder: (context, size) {
                      var text = "负责人：${saleProject['applyEmp']}";
                      var textStyle = TextStyle(color: Color(0xff666666), fontSize: 28.sp);
                      var _widget;
                      _widget = Text(text, style: textStyle, overflow: TextOverflow.ellipsis);
                      var tp = TextPainter(
                        maxLines: 1,
                        textAlign: TextAlign.left,
                        textDirection: TextDirection.ltr,
                        text: TextSpan(text: text, style: textStyle),
                      )..layout(maxWidth: size.maxWidth);
                      var exceeded = tp.didExceedMaxLines;
                      if (exceeded) {
                        _widget = Tooltip(message: "${saleProject['applyEmp']}", child: _widget);
                      }
                      return _widget;
                    },
                  ),
                ),
                Text(
                  '立项日期：${saleProject['applyDate']}',
                  style: TextStyle(color: Color(0xff666666), fontSize: 28.sp),
                ),
              ],
            ),
            // SizedBox(height: 28.h),
            // Divider(height: 1.w),
            // SizedBox(height: 30.h),
            // Row(
            //   crossAxisAlignment: CrossAxisAlignment.start,
            //   children: [
            //     Container(
            //       width: 424.w,
            //       child: Row(
            //         crossAxisAlignment: CrossAxisAlignment.start,
            //         children: [
            //           Container(
            //             width: 115.w,
            //             child: Text(
            //               '进度描述:',
            //               style: TextStyle(color: Color(0xff999999), fontSize: 24.sp),
            //             ),
            //           ),
            //           Flexible(
            //             child: Column(
            //               children: [
            //                 Text(
            //                   'xxxxxxxxxxxxxxxxxxxxxxxxxXXXXXXXXXxxxxxxxxxxxxxxxx',
            //                   softWrap: true,
            //                   // overflow: TextOverflow.ellipsis,
            //                   style: TextStyle(color: Color(0xff999999), fontSize: 24.sp),
            //                 ),
            //               ],
            //             ),
            //           )
            //         ],
            //       ),
            //     ),
            //     Spacer(),
            //     Container(
            //       child: Row(
            //         children: [
            //           LoadAssetImage('sale_project/edit', width: 33.w, height: 30.h),
            //           SizedBox(width: 40.w),
            //           LoadAssetImage('sale_project/arrow_down', width: 30.w, height: 18.h),
            //           SizedBox(width: 7.w)
            //         ],
            //       ),
            //     ),
            //   ],
            // ),
            // SizedBox(height: 17.h),
            // Text(
            //   '2021-03-10 14:24:51',
            //   style: TextStyle(color: Color(0xff999999), fontSize: 24.sp),
            // ),
          ],
        ),
      ),
    );
  }
}
